from django.urls import path
from stark.service.s1 import StarkHandler, choice_display
from app_main import models


# confirm_date, confirm_er 两个字段的完善功能 未完成
class CheckPayRecordHandler(StarkHandler):
    list_display = [
        StarkHandler.display_checkbox,
        'customer',
        'consultant',
        choice_display('费用类型', 'pay_type'),
        'money',
        'to_class',
        'apply_date',
        choice_display('确认状态', 'confirm_status')
    ]

    my_sort = ['confirm_status']

    has_add_btn = False

    def get_urls(self):
        patterns = [
            path('list/', self.wrapper(self.list_view), name=self.get_list_url_name),
        ]

        patterns.extend(self.extra_urls())

        return patterns

    def multi_confirm(self, request, *args, **kwargs):
        id_list = request.POST.getlist('cb')
        for pid in id_list:
            p_obj = models.PaymentRecord.objects.filter(id=pid).first()
            p_obj.confirm_status = 2
            p_obj.save()

            p_obj.customer.status = 1
            p_obj.customer.save()

            p_obj.customer.student.student_status = 2
            p_obj.customer.student.save()

    multi_confirm.text = '批量确认'

    def multi_cancel(self, request, *args, **kwargs):
        id_list = request.POST.getlist('cb')
        models.PaymentRecord.objects.filter(id__in=id_list).update(confirm_status=3)

    multi_cancel.text = '批量驳回'

    action_list = [multi_confirm, multi_cancel]
